home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_2.lha / 8_2 / 8_2c.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  1KB  |  57 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / check a line to make sure it is made
  6. / up entirely of spaces and tabs
  7. include <ctype.h>
  8. tatic int isblankline(char *buf)
  9.  
  10.    for ( ; *buf; buf++)
  11. if (!isspace(*buf))
  12.     return 0;
  13.  
  14.    return 1;
  15.  
  16.  
  17. / Input a series of lines into a name and address.
  18. stream& operator>>(istream &in, name_and_address &x)
  19.  
  20.    x.deallocate_name();
  21.    x.name = new char*[x.len = 6];
  22.  
  23.    char **n = x.name;
  24.    for (int curlen = 0; ; curlen++, n++)
  25. {
  26. // read in a line
  27. char buf[256], c;
  28. in.get(buf, sizeof(buf));
  29. in.get(c);    // zap the newline
  30. if (!in)
  31.     break;
  32.  
  33. // did we reach a blank line?
  34. if (isblankline(buf))
  35.     break;
  36.  
  37. // reallocate if necessary
  38. if (curlen >= x.len)
  39.     {
  40.     const int inc = 4;
  41.     char **svname = x.name;
  42.     x.name = new char*[x.len + inc];
  43.     memcpy((char*)x.name, (char*)svname,
  44.     x.len * sizeof(char*));
  45.     x.len += inc;
  46.     }
  47.  
  48. // save the line
  49. *n = new char[strlen(buf) + 1];
  50. strcpy(*n, buf);
  51. }
  52.  
  53.    *n = 0;
  54.    return in;
  55.  
  56.  
  57.